package chenji.watcher.listeners;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import chenji.watcher.Main;
import chenji.watcher.core.WatchGroupData;

public class CommandWatcher implements CommandExecutor {

	@Override
	public boolean onCommand(CommandSender arg0, Command arg1, String arg2, String[] arg3) {
		if (arg3.length == 1) {
			if (arg3[0].equalsIgnoreCase("play")) {
				arg0.sendMessage("§a----------------------§cWatcher 播放器使用帮助§a---------------------");
				arg0.sendMessage("§c/Watcher play §b[名称] §a为执行的玩家播放一个观察组");
				arg0.sendMessage("§c/Watcher play §b[玩家名称] [名称] §a为一个玩家播放一个观察组");
				arg0.sendMessage("§a--------------------------------------------------------------------");
				return true;
			}
		}
		if (arg3.length == 2) {
			if (arg3[0].equalsIgnoreCase("play")) {
				if (arg0 instanceof Player) {
					if (arg0.hasPermission("Watcher.Play." + arg3[1])) {
						this.play((Player) arg0, arg3[1], arg0);
						return true;
					} else {
						arg0.sendMessage("§c[Watcher] §a你没有权利这样做 -§b" + "Watcher.Play." + arg3[1]);
						return true;
					}
				} else {
					arg0.sendMessage("只有玩家才可以这样干");
					return true;
				}
			}
		}
		if (arg3.length == 3) {
			if (arg3[0].equalsIgnoreCase("play")) {
				if (!(arg0.hasPermission("Watcher.SudoPlay"))) {
					arg0.sendMessage("§c[Watcher] §a你没有权利这样做 -§bWatcher.SudoPlay");
					return true;
				}
				Player p = Bukkit.getPlayer(arg3[1]);
				if (p == null) {
					arg0.sendMessage("§c[Watcher] §a对应玩家不存在");
					return true;
				}
				if (!p.getName().equalsIgnoreCase(arg3[1])) {
					arg0.sendMessage("§c[Watcher] §a对应玩家不存在");
					return true;
				}
				this.play(p, arg3[2], arg0);
				arg0.sendMessage("§c[Watcher] §a播放成功");
				return true;
			}
		}
		if (!(arg0 instanceof Player)) {
			arg0.sendMessage("[Watcher] 只有玩家才可以进行这个操作");
			return true;
		}
		if (!(arg0.hasPermission("Watcher.admin"))) {
			arg0.sendMessage("你没有权限这样做 -§bWatcher.admin");
			return true;
		}
		Player p = (Player) arg0;
		if (arg3.length == 0) {
			p.sendMessage("§a----------------------§cWatcher 编辑器使用帮助§a---------------------");
			p.sendMessage("§c/Watcher create §b[名称] §a创建一个观察组。");
			p.sendMessage("§c/Watcher edit §b[名称] §a编辑一个观察组");
			p.sendMessage("§c/Watcher setStartTitle §b[字幕] §a设置一个观察组的开始字幕");
			p.sendMessage("§c/Watcher setStartTime §b[等待时间] §a设置一个观察组开始的等待时间");
			p.sendMessage("§c/Watcher setEndTitle §b[字幕] §a设置一个观察组的结束的字幕");
			p.sendMessage("§c/Watcher setEndTime §b[等待时间] §a设置一个观察组的结束的等待时间");
			p.sendMessage("§c/Watcher setCanBack §b[true/false] §a设置玩家当播放结束后是否回到起始位置");
			p.sendMessage("§c/Watcher addNT §b[等待时间] [名称] [字幕（可选）] §a为观察组添加一个传送点");
			p.sendMessage("§c/Watcher remNT §b[名称] §a为观察组删除一个传送点。");
			p.sendMessage("§c/Watcher save §a保存编辑中的观察组");
			p.sendMessage("§c/Watcher remove §b[名称] §a删除一个观察组");
			p.sendMessage("§c/Watcher list §a获取当前观察组的信息");
			p.sendMessage("§c/Watcher list §b[名称] §a获取传送组的信息");
			p.sendMessage("§a--------------------------------------------------------------------");
			return true;
		}

		String head = arg3[0];

		if (arg3.length == 1) {
			if (head.equalsIgnoreCase("save")) {
				Main.getWatchGroupEditorManager().save(p);
				return true;
			}
			if (head.equalsIgnoreCase("list")) {
				Main.getWatchGroupEditorManager().print(p);
				return true;
			}

		}
		if (arg3.length == 2) {
			if (head.equalsIgnoreCase("create")) {
				Main.getWatchGroupEditorManager().createWatchGroup(p, arg3[1]);
				return true;
			}
			if (head.equalsIgnoreCase("remove")) {
				Main.getWatchGroupEditorManager().removeWatchGroup(arg3[1], p);
				return true;
			}
			if (head.equalsIgnoreCase("list")) {
				Main.getWatchGroupEditorManager().print(p, arg3[1]);
				return true;
			}
			if (head.equalsIgnoreCase("edit")) {
				String name = arg3[1];
				WatchGroupData wgd = Main.getWatchGroupManager().getWatchGroupData(name);
				if (wgd == null) {
					arg0.sendMessage("§c[Watcher] §a那个观察组不存在");
					return true;
				}
				Main.getWatchGroupEditorManager().editWatchGroupData(p, wgd);
				p.sendMessage("§c[Watcher] §a成功绑定，现在你可以开始编辑这个组了，如果需要请输入/watcher 获取编辑教程");
				return true;
			}
			if (head.equalsIgnoreCase("setStartTitle")) {
				Main.getWatchGroupEditorManager().setStartTitle(p, arg3[1]);
				return true;
			}
			if (head.equalsIgnoreCase("setStartTime")) {
				int i = this.getInt(arg3[1]);
				if (i == -1) {
					p.sendMessage("§c[Watcher] §a请输入正确的数字。");
					return true;
				}
				Main.getWatchGroupEditorManager().setStartTime(p, i);
				return true;
			}

			if (head.equalsIgnoreCase("setEndTitle")) {
				Main.getWatchGroupEditorManager().setEndTitle(p, arg3[1]);
				return true;
			}
			if (head.equalsIgnoreCase("setEndTime")) {
				int i = this.getInt(arg3[1]);
				if (i == -1) {
					p.sendMessage("§c[Watcher] §a请输入正确的数字。");
					return true;
				}
				Main.getWatchGroupEditorManager().setEndTime(p, i);
				return true;
			}
			if (head.equalsIgnoreCase("setCanBack")) {
				boolean b;
				try {
					b = Boolean.valueOf(arg3[1]);
				} catch (Exception ex) {
					p.sendMessage("§c[Watcher] §a§c请输入正确的布尔值（true/false)。");
					return true;
				}
				Main.getWatchGroupEditorManager().setBack(p, b);
			}
			if (head.equalsIgnoreCase("remNT")) {
				Main.getWatchGroupEditorManager().removeTeleporter(arg3[1], p);
				return true;
			}

		}
		if (arg3.length == 3) {
			if (head.equalsIgnoreCase("addNT")) {
				int i = this.getInt(arg3[1]);
				if (i == -1) {
					p.sendMessage("§c[Watcher] §a请输入正确的数字。");
					return true;
				}
				Main.getWatchGroupEditorManager().addNewTeleporter(p, arg3[2], "", i, p.getLocation());
				return true;
			}
		}

		if (arg3.length == 4) {
			if (head.equalsIgnoreCase("addNT")) {
				int i = this.getInt(arg3[1]);
				if (i == -1) {
					p.sendMessage("§c[Watcher] §a请输入正确的数字。");
					return true;
				}
				Main.getWatchGroupEditorManager().addNewTeleporter(p, arg3[2], arg3[3], i, p.getLocation());
				return true;
			}
		}
		p.sendMessage("§c[Wather] §a无效的命令参数。");
		return true;
	}

	private int getInt(String s) {
		int i;
		try {
			i = Integer.valueOf(s);
		} catch (Exception ex) {
			return -1;
		}
		return i;
	}

	private void play(Player p, String Name, CommandSender sender) {
		WatchGroupData wgd = Main.getWatchGroupManager().getWatchGroupData(Name);
		if (wgd == null) {
			sender.sendMessage("§c[Watcher] §a那个观察组不存在");
		} else {
			Main.getWatchGroupManager().play(p, wgd);
		}
	}
}
